<?php
/**
 * Created by PhpStorm.
 * User: Yan
 * Date: 2017/9/28
 * Time: 13:43
 */

namespace App\Http\Controllers\API;


use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class OrgController extends Controller
{
    public function order(){
        //\Cache::flush();
        if (!\Cache::get('organizationals')) {
            $companyList = $this->getOrganizationalOrder();
            \Cache::put('organizationals', json_encode($companyList, JSON_UNESCAPED_UNICODE),3600);
        }
        $cache = \Cache::get('organizationals');
        return $cache;
    }
    public function select($pk = 'c61920b2825a4f1c96d0ebd7db5436d7'){
        $res = [];
        $result = DB::table('organizational_structures')->where('key',$pk)->first();
        if($result){

            $org =DB::table('organizational_structures')->where('level_tab', 'like','%-'.$result->id.'-%')->get();
            foreach ($org as $item){
                $res[$item->id] = $item->organizational_name;
            }

        }
        return $res;
    }
    public function getOrganizationalOrder($pk = 'c61920b2825a4f1c96d0ebd7db5436d7'){
        $result = [];
        $org = DB::table('organizational_structures')->where('parent_key',$pk)->get();
        foreach ($org as $item){
            $res['label'] = $item->organizational_name;
            $res['id'] = $item->id;
            $res['open'] = false;
            $res['checked'] =false;
            $res['nodeSelectNotAll'] = false;
            $res['parentId']=$item->parent_key;
            $res['visible']=true;
            $children = $this->getOrganizationalOrder($item->key);
            if($children){
                $res['children']=$children;
            }
            $result[]=$res;
        }
        return $result;
    }

}